<?php

if ( !function_exists( 'member_posttype_init' ) ) {
	/**
	 * Register member posttype for manage member of team
	 * @return void
	 */
	function member_posttype_init() {

		$member_labels = array(
			'name' => _x('Members', 'post type general name', 'cingle'),
			'singular_name' => _x('Member', 'post type singular name', 'cingle'),
			'add_new' => _x('Add New', 'Member'),
			'add_new_item' => __('Add New Member'),
			'edit_item' => __('Edit Member'),
			'new_item' => __('New Member'),
			'all_items' => __('All Members'),
			'view_item' => __('View Member'),
			'search_items' => __('Search Members'),
			'not_found' =>  __('No Members found'),
			'not_found_in_trash' => __('No Members found in Trash'), 
			'parent_item_colon' => '',
			'menu_name' => __('Members')
		);

		$member_args = array(
			'labels' => $member_labels,
			'public' => false,
			'publicly_queryable' => false,
			'show_ui' => true, 
			'show_in_menu' => true, 
			'query_var' => false,
			'rewrite' => false,
			'capability_type' => 'post',
			'has_archive' => false, 
			'hierarchical' => false,
			'menu_position' => '5.1',
			'menu_icon' => CINGLE_URI . 'img/posttype-member.png',
			'supports' => array( 'title', 'thumbnail' )
		); 
		register_post_type( 'member', $member_args );

	}
	add_action( 'init', 'member_posttype_init' );
}


function member_meta_box(){
	add_meta_box( 'member-infomation', 'Member Profile', 'member_metabox_calback', 'member', 'normal' );
}
add_action( 'admin_init', 'member_meta_box' );


function member_metabox_calback($post){
	$post_meta = wp_parse_args( get_post_meta( $post->ID ), array(
		'name'				=> '',
		'email'				=> '',
		'url'				=> '',
		'description'		=> '',
		'description'		=> '',
		'social_facebook'	=> '',
		'social_twitter'	=> '',
		'social_gplus'		=> '',
		'social_dribbble'	=> '',
		'social_vimeo'		=> '',
		'social_flickr'		=> '',
	) ) ;

?>
<table width="100%">
	<tr class="alternate">
		<td>Name</td>
		<td><input type="text" value="<?php echo $post_meta['name'][0] ?>" name="user_meta[name]" class="regular-text" ></td>
	</tr>
	<tr class="alternate">
		<td>Email</td>
		<td><input type="text" value="<?php echo $post_meta['email'][0] ?>" name="user_meta[email]" class="regular-text" ></td>
	</tr>
	<tr class="alternate">
		<td>Website</td>
		<td><input type="text" value="<?php echo $post_meta['url'][0] ?>" name="user_meta[url]" class="regular-text" ></td>
	</tr>
	<tr class="alternate">
		<td>Description</td>
		<td><textarea name="user_meta[description]" id="user_description" class="large-text" rows="10"><?php echo $post_meta['description'][0]; ?></textarea></td>
	</tr>
	<tr>
		<td cols="2">
			<p><strong>Social infomations</strong></p>
		</td>
	</tr>
	<tr class="alternate">
		<td>Facebook</td>
		<td><input type="text" value="<?php echo $post_meta['social_facebook'][0] ?>" name="user_meta[social_facebook]" class="regular-text" ></td>
	</tr>
	<tr class="alternate">
		<td>Twitter</td>
		<td><input type="text" value="<?php echo $post_meta['social_twitter'][0] ?>" name="user_meta[social_twitter]" class="regular-text" ></td>
	</tr>
	<tr class="alternate">
		<td>Google Plus</td>
		<td><input type="text" value="<?php echo $post_meta['social_gplus'][0] ?>" name="user_meta[social_gplus]" class="regular-text" ></td>
	</tr>
	<tr class="alternate">
		<td>Dribbble</td>
		<td><input type="text" value="<?php echo $post_meta['social_dribbble'][0] ?>" name="user_meta[social_dribbble]" class="regular-text" ></td>
	</tr>
	<tr class="alternate">
		<td>Vimeo</td>
		<td><input type="text" value="<?php echo $post_meta['social_vimeo'][0] ?>" name="user_meta[social_vimeo]" class="regular-text" ></td>
	</tr>
	<tr class="alternate">
		<td>Flickr</td>
		<td><input type="text" value="<?php echo $post_meta['social_flickr'][0] ?>" name="user_meta[social_flickr]" class="regular-text" ></td>
	</tr>
</table>
<?php
}

function rb_member_save_meta( $post_id  ){
	if( ! wp_is_post_revision($post_id) ){
		foreach ($_POST["user_meta"] as $key =>  $usermeta ) {
			update_post_meta( $post_id , $key, $_POST['user_meta'][$key] );
		}
	}
}
add_action( 'save_post', 'rb_member_save_meta' );

?>